JavaScript syntax
part 8/31 Β· 107.4 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
There are two ways to declare a BigInt value. An n can be appended to an integer, or the BigInt function can be used:cite-ref-bigint-mdn-11-1[11]
const a = 12345n; // Creates a variable and stores a BigInt value of 12345
const b = BigInt(12345);
String
A string in JavaScript is a sequence of characters. In JavaScript, strings can be created directly (as literals) by placing the series of characters between double (") or single (') quotes. Such strings must be written on a single line, but may include escaped newline characters (such as \n). The JavaScript standard allows the backquote character (`, a.k.a. grave accent or backtick) to quote multiline literal strings, as well as embedded expressions using the syntax ${expression}.cite-ref-12[12]
const greeting = "Hello, World!";
const anotherGreeting = 'Greetings, people of Earth.';
const aMultilineGreeting = `Warm regards,
John Doe.`
// Template literals type-coerce evaluated expressions and interpolate them into the string.
const templateLiteral = `This is what is stored in anotherGreeting: ${anotherGreeting}.`;
console.log(templateLiteral); // 'This is what is stored in anotherGreeting: 'Greetings, people of Earth.''
console.log(`You are ${Math.floor(age)=>18 ? "allowed" : "not allowed"} to view this web page`);
Individual characters within a string can be accessed using the charAt method (provided by String.prototype). This is the preferred way when accessing individual characters within a string, because it also works in non-modern browsers:
const h = greeting.charAt(0);
In modern browsers, individual characters within a string can be accessed (as strings with only a single character) through the same notation as arrays:
const h = greeting[0];
However, JavaScript strings are immutable:
greeting[0] = "H"; // Fails.
Applying the equality operator ("==") to two strings returns true, if the strings have the same contents, which means: of the same length and containing the same sequence of characters (case is significant for alphabets). Thus:
const x = "World";
const compare1 = ("Hello, " + x == "Hello, World"); // Here compare1 contains true.
const compare2 = ("Hello, " + x == "hello, World"); // Here compare2 contains ...
// ... false since the ...
// ... first characters ...
// ... of both operands ...
// ... are not of the same case.
Quotes of the same type cannot be nested unless they are escaped.
let x = '"Hello, World!" he said.'; // Just fine.
x = ""Hello, World!" he said."; // Not good.
x = "\"Hello, World!\" he said."; // Works by escaping " with \"
The String constructor creates a string object (an object wrapping a string):
const greeting = new String("Hello, World!");
These objects have a valueOf method returning the primitive string wrapped within them:
const s = new String("HelloΒ !");
typeof s; // Is 'object'.
typeof s.valueOf(); // Is 'string'.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ